<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="../../common/import.jsp" %>

<html>
  <head>
    <script type="text/javascript">
	   $(function(){
		   selectCheckAll();
		   $("input[name='roleList']").click(function() {
			   selectCheckAll();
		   });
	   })
	   function selectAll(checkbox){
		   $('input[type=checkbox]').prop('checked', $(checkbox).prop('checked'));
	   }
	   function selectCheckAll(){
		   var $subs = $("input[name='roleList']");
		   $("#chk_all").prop("checked" , $subs.length == $subs.filter(":checked").length ? true :false);
	   }
       function onSubmit(){
    	   var roleIds = "";
    	   $("input[name='roleList']:checked").each(function(){ 
    		   roleIds += $(this).val()+",";
    	   })
    	   if(roleIds.length>0){
    		   roleIds = roleIds.substring(0,roleIds.length-1);
    	   }
    	   $.blockUI();
    		   $.ajax({
	    			  url: "${ctx}/sys/user/saveUserRoles.do",   
	    		      data: {userId: '${userId}', roleIds: roleIds}, 
	    		      type: "POST",
	    		      success: function(data) {
	    		    	  $.unblockUI();
	    		    	  if(data.success==true){
	    		        		showSuccessMsg();
	    		        		//parent.reloadGrid();
	    		        		parent.closeWindow();
	    		        	}else{
	    		        		showFailAlert(data.msg)
	    		        	}
	    		      },
	    		      error: function(xhr) {
	    		    	  $.unblockUI();
	    		    	  showFailAlert2(xhr);
	    		      }
	    		  })
       }
    </script>
</head>
  <body class="easyui-layout" scroll="no">
    <div region="center" style="padding: 10px;" border="false">
       <c:choose>  
	       <c:when test="${not empty userRoles}">
	           <input type="checkbox" name="chk_all" id="chk_all" onclick="selectAll(this);"/><label for="chk_all" style="cursor:pointer">全选/取消</label><br/>
	           <c:forEach items="${userRoles}" var="userRoles" >
	              <c:if test="${empty userRoles.USER_ID }">
	                 <input id="${userRoles.ID}" name="roleList" type="checkbox" value="${userRoles.ID}" /><label for="${userRoles.ID}" style="cursor:pointer">${userRoles.ROLE_NAME}</label>
	              </c:if>
	              <c:if test="${userRoles.USER_ID == userId}">
	                 <input id="${userRoles.ID}" name="roleList" type="checkbox" checked="checked" value="${userRoles.ID}" /><label for="${userRoles.ID}" style="cursor:pointer">${userRoles.ROLE_NAME}</label>
	              </c:if>
		       </c:forEach>
	       </c:when>
	       <c:otherwise>
	           <label style="color: red">没有角色可以分配，请先添加角色</label>
	       </c:otherwise>
      </c:choose>
    </div>
    <div region="south" border="false" style="text-align: right; padding: 5px 5px 5px 0;">
			<a class="easyui-linkbutton" iconcls="icon-ok" href="javascript:void(0)" onclick="onSubmit();">提交</a>
			<a class="easyui-linkbutton" iconcls="icon-cancel" href="javascript:void(0)" onclick="parent.closeWindow();">取消</a>
	</div>
  </body>
</html>
